﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Battle
{
    public class LevelScript_Normal : ILevelScript
    {
        /// <summary>
        /// 小怪刷新间隔
        /// </summary>
        protected const float NormalInterval = 0.2f;
        /// <summary>
        /// 大波小怪
        /// </summary>
        protected const float GroupInterval = 10f;
        /// <summary>
        /// 精英怪
        /// </summary>
        protected const float BigInterval = 15f;
        /// <summary>
        /// boss间隔
        /// </summary>
        protected const float BossInterval = 30f;
        protected const float LevelTime = 45f;

        protected const float NormalSpeed = 1, BigSpeed = 0.75f, BossSpeed = 0.5f;

        protected bool bClose;
        protected int nLevel;

        protected Vector3 RandomBornPos => new Vector3(Random.Range(-1f, 1f) * GameMgr.ScreenSize.x, GameMgr.ScreenSize.y);
        protected Vector3 CenterBornPos => new Vector3(0, GameMgr.ScreenSize.y);

        protected int normalHP, bigHP, bossHP;

        private float timeLevel, timeNormal, timeGroup, timeBig, timeBoss;

        public void StartLevel(int level)
        {
            bClose = false;
            nLevel = level;

            normalHP = 1 + 10 * level;
            bigHP = 10 + 100 * level;
            bossHP = 100 + 1000 * level;

            timeBig = Time.time;
            timeBoss = Time.time;
            timeGroup = Time.time;
            timeNormal = Time.time;
            timeLevel = Time.time;
        }

        public void OnUpdate(float deltaTime)
        {
            if (bClose) return;

            if (Time.time > timeNormal + NormalInterval)
            {
                timeNormal += NormalInterval;
                CreateNormalEnemy();
            }
            if (Time.time > timeGroup + GroupInterval)
            {
                timeGroup += GroupInterval;
                CreateGroupAsync();
            }
            if (Time.time > timeBig + BigInterval)
            {
                timeBig += BigInterval;
                CreateBigEnemy();
            }
            if (Time.time > timeBoss + BossInterval)
            {
                timeBoss += BossInterval;
                CreateBossEnemy();
            }

            if (Time.time > timeLevel + LevelTime)
                LevelScriptMgr.NextLevel();
        }

        public void StopLevel()
        {
            bClose = true;
        }

        public void CreateNormalEnemy()
        {
            //Debug.Log("CreateNormalEnemy");

            EnemyFactory.CreateNormalEnemy(RandomBornPos, normalHP, NormalSpeed * Random.Range(0.7f, 1.7f));
        }
        public async void CreateGroupAsync()
        {
            //Debug.Log("CreateGroupAsync");

            int interval = (int)(NormalInterval * 1000);
            int count = 50;
            while (!bClose && count < 0)
            {
                for (int i = 0; i < 5; i++)
                {
                    CreateNormalEnemy();
                    count--;
                }

                await Task.Delay(interval);
            }
        }
        public void CreateBigEnemy()
        {
            //Debug.Log("CreateBigEnemy");

            EnemyFactory.CreateNormalEnemy(RandomBornPos, bigHP, BigSpeed * Random.Range(0.9f, 1.1f));
        }
        public void CreateBossEnemy()
        {
            //Debug.Log("CreateBossEnemy");

            EnemyFactory.CreateNormalEnemy(CenterBornPos, bossHP, BossSpeed * Random.Range(0.9f, 1.1f));
        }
    }
}
